package org.example.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class SessionUtil {

    private final String prefixKey = "login:user:";
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    public void setSession(String token,Integer id){
        if(token != null){
            //setnx ex
            redisTemplate.opsForValue().setIfAbsent(prefixKey+id,token,1000*60*30L, TimeUnit.MILLISECONDS);
        }
    }

    public String getToken(Integer id){
        return (String)redisTemplate.opsForValue().get(prefixKey+id);
    }
}
